package com.woniu.service.Impl.homePage;

import cn.hutool.db.sql.Order;
import com.woniu.dao.backpage.GoodsDao;
import com.woniu.dao.homePage.SortingAppDao;
import com.woniu.entity.*;
import com.woniu.service.homePage.ISortingAppService;
import com.woniu.util.ResData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @author 陈乙嘉
 * @data 2023/3/12 14:26
 */

@Service
public class SortingAppServiceImpl implements ISortingAppService {

    @Autowired(required = false)
    private SortingAppDao sortingAppDao;

    @Autowired(required = false)
    private GoodsDao goodsDao;

    @Override
    public ResData<?> getSortingOrderCount(Integer sorterId, Date beginTime, Date endTime) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        List<Integer> sortingOrderCount = sortingAppDao.getSortingOrderCount(sorterId, simpleDateFormat.format(beginTime),
                    simpleDateFormat.format(endTime));

        return new ResData<>().ok(sortingOrderCount.size());

    }

    @Override
    public ResData<?> findOrdersByConditions(Integer sorterId,Integer status) {
        List<Order> orders = sortingAppDao.findOrdersByConditions(sorterId, status);
        return new ResData<>().ok(orders);
    }

    @Override
    public ResData<?> updateOrderStatus(Orders orders) {
        sortingAppDao.updateOrderStatus(orders);
        return new ResData<>().ok();
    }

    @Override
    public ResData<?> findOrdersDetailsByOrderId(String orderId) {
        List<OrdersDetails> ordersDetails = sortingAppDao.findOrdersDetailsByOrderId(orderId);
        return new ResData<>().ok(ordersDetails);
    }

    @Override
    public ResData<?> updateSorterId(Orders orders) {
        sortingAppDao.updateSorterId(orders);
        return new ResData<>().ok();
    }

    @Override
    public ResData<?> sorterLogin(String phone, String password) {
        Integer integer = sortingAppDao.sorterLogin(phone, password);
        if(integer>0){
            return new ResData<>().ok();
        }
        return ResData.LOGIN_FILA;
    }

    @Override
    public ResData<?> getSorterIdByPhone(String phone) {
        Integer sorterIdByPhone = sortingAppDao.getSorterIdByPhone(phone);
        return new ResData<>().ok(sorterIdByPhone);

    }

    @Override
    public ResData<?> queryWorkUser(String phone) {
        WorkUser workUser = sortingAppDao.queryWorkUser(phone);
        return new ResData<>().ok(workUser);
    }

    @Override
    public ResData<?> updatePasswordByPhone(String phone, String password) {
        sortingAppDao.updatePasswordByPhone(phone,password);
        return  new ResData<>().ok();
    }


    //根据分拣员id 和订单号查询所有分拣信息
    @Override
    public ResData<?> findSorting(String orderId,Integer sorterId) {
        Sorting sorting = sortingAppDao.findSorting(orderId,sorterId);
        return  new ResData<>().ok(sorting);
    }

    //插入分拣表信息
    @Override
    public ResData<?> insertSorting(Sorting sorting) {
        sortingAppDao.insertSorting(sorting);
        return new ResData<>().ok();
    }

    //当分拣已完成修改分拣表状态
    @Override
    public ResData<?> updateSorting(Integer state,String orderId, Integer userId) {
        sortingAppDao.updateSorting(state,orderId, userId);
        return new ResData<>().ok();

    }

    @Override
    public ResData<?> insertImage(Image image) {
        goodsDao.insertImage(image);
        return new ResData<>().ok();
    }

    @Override
    public ResData<?> insertGoodsImage(GoodsImage goodsImage) {
        goodsDao.insertGoodsImage(goodsImage);
        return new ResData<>().ok();
    }

    @Override
    public ResData<?> findImageByImgUrl(Image image) {
        Image imageByImgUrl = goodsDao.findImageByImgUrl(image);
        return new ResData<>().ok(imageByImgUrl);
    }
}
